package it.sd.pwframework.admin.bean;

import it.sd.pwframework.business.bean.PwForm;
import it.sd.pwframework.business.bean.PwProperty;
import it.sd.pwframework.business.bean.PwRow;
import it.sd.pwframework.business.bean.PwRows;
import it.sd.pwframework.business.bean.PwValues;
import it.sd.pwframework.business.service.CommonService;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import javax.persistence.Transient;

@ManagedBean(name="crudBean")
@ViewScoped
public class CrudJsfBean extends AbstractJsfBean {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@ManagedProperty(value="#{commonService}")
	@Transient
    private transient CommonService commonService;

	@ManagedProperty(value="#{selectorsJsfBean}")
	@Transient
    private transient SelectorsJsfBean selectorsJsfBean;

	public CommonService getCommonService() {
		return commonService;
	}

	public void setCommonService(CommonService commonService) {
		this.commonService = commonService;
	}
	
	private String idForm;
	private PwForm form;
	private PwRow row;
	
	public String getIdForm() {
		return idForm;
	}

	public void setIdForm(String idForm) {
		this.idForm = idForm;
		init();
	}
	
	public PwForm getForm() {
		return form;
	}

	public void setForm(PwForm form) {
		this.form = form;
	}

	private PwRows values;
	
	public void init() {
		try {
			form = commonService.getForm(idForm);
			loadRows();
		} catch(Exception e ){
			log.error("error loading entities", e);
		}
	}

	private void loadRows() {
		try {
			values = commonService.retrieveRows(form);
		} catch(Exception e ){
			log.error("error loading entities", e);
		}
	}

	public PwRows getValues() {
		return values;
	}

	public void setValues(PwRows values) {
		this.values = values;
	}

	public PwRow getRow() {
		return row;
	}

	public void setRow(PwRow row) {
		this.row = row;
	}

	
	public void setId(Long id) {
		try {
			row = commonService.retrieveRow(form, id);
		} catch(Exception e) {
			log.error("error retrieving row: " + id, e);
		}
	}
	
	private boolean insertMode;
	
	public void doNew() {
		
		row = new PwRow(form);
		row.setValues(new PwValues());
		insertMode = true;
	}
	
	public void doEdit() {
		insertMode = false;
		
	}

	public boolean getInsertMode() {
		return insertMode;
	}
	
	public void doDelete() {
		try {
			commonService.delete(row);
			showInfoMessage("Salvataggio completato");
			addCallBackOk();
		} catch (Exception e) {
			showErrorMessage("Errore durante salvataggio", e);

		}
		loadRows();
	}
	
	public void doSave() {
		try {
//			if (doValidate()) {
				commonService.save(row);
				showInfoMessage("Salvataggio completato");
				addCallBackOk();
				loadRows();
//			}
		} catch (Exception e) {
			showErrorMessage("Errore durante salvataggio", e);

		}
	}

	public boolean doValidate() {
		
		boolean esito = true;
		
		for (PwProperty prop : form.getProperties()) {
			String validatorname = prop.getValidator();
			if ( (validatorname!=null) && (validatorname.trim().length()>0)) {
				Validator validator = null;
				try {
					validator = (Validator) FacesContext.getCurrentInstance().getApplication().createValidator(validatorname);
					validator.validate(FacesContext.getCurrentInstance(), null, this.row.getValues().get(prop.getName()));
				} catch(ValidatorException e) {
					FacesContext.getCurrentInstance().addMessage(null, e.getFacesMessage());
					esito = false;
				} catch(Exception e) {
					log.error("error creating validator:" + validatorname, e);
				}
				
			}
			
		}
		
		return esito;
	       
	}
	
	public SelectorsJsfBean getSelectorsJsfBean() {
		return selectorsJsfBean;
	}

	public void setSelectorsJsfBean(SelectorsJsfBean selectorsJsfBean) {
		this.selectorsJsfBean = selectorsJsfBean;
	}

	
	
	
	
	
}
